#include "font.h"

inline int next_p2(int a) { int rval = 1; while(rval < a) rval <<=1; return rval; }

namespace sleek
{
    namespace gui
    {
        font::font(core::u8*data, core::s32 size)
        {
            fonts = 0;
        }
        font::font(core::os::stringc ttf)
        {
            file = ttf;
            fonts = new FTTextureFont(file.c_str());
            if(fonts->Error()) printf("Can't load TTF %s\n", file.c_str());
            color = texture::pixel(255,255,255,255);
            fonts->Depth(10);
            fonts->Outset(0, 3);
            fonts->CharMap(ft_encoding_unicode);
            setSize(10);
        }
        font::~font()
        {
            if(fonts) delete fonts;
        }

        void font::make(core::os::stringc t)
        {
            if(!fonts) return;
            fonts->Advance(t.c_str());
            text = t;
        }
        core::math::vector2di font::getBoxSize()
        {
            if(!fonts) return core::math::vector2di(0,0);
            FTPoint ft = fonts->BBox(text.c_str(), text.size()).Upper();
            return core::math::vector2di((int)ft.X(),(int)ft.Y());
        }
        font* font::clone()
        {
            if(!fonts) return 0;
            font *tmp = new font(file);
                tmp->make(text);
                tmp->setColor(color);
                tmp->setSize(size);
            return tmp;
        }
        void font::setColor(texture::pixel i)
        {
            color = i;
        }
        texture::pixel font::getColor()
        {
            return color;
        }
        void font::setSize(core::u32 h)
        {
            size = h;
            if(!fonts) return;
            if(!fonts->FaceSize(size)) printf("Can't resize font\n");
        }
        core::u32 font::getSize()
        {
            return size;
        }
        void font::render()
        {
            if(!fonts) return;
            fonts->Render(text.c_str(), text.size());
        }
    }
}
